package com.itheima.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.itheima.sentinel.service.SentinelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Class: Sentinel案例测试
 * @Package com.itheima.sentinel.controller
 * @Description:
 * @Company: http://www.itheima.com/
 */
@RestController

public class SentinelController {
    private static final Logger log = LoggerFactory.getLogger(SentinelController.class);
    @Autowired
    SentinelService sentinelService;
    
    @GetMapping(value = "/sn/test")
    public String hello() {
        return "发送心跳包..";
    }
    
    /*
     * @Description: qps+直接+快速失败测试
     * @Method: order
     * @Param: []
     * @Update:
     * @since: 1.0.0
     * @Return: java.lang.String
     *
     */
    @GetMapping("/order")
    public String order() {
        return "订单创建成功...";
    }
    
    /*
     * @Description: qps+关联+快速失败测试
     * @Method: payment
     * @Param: []
     * @Update:
     * @since: 1.0.0
     * @Return: java.lang.String
     *
     */
    @GetMapping("/payment")
    public String payment() {
        return "支付成功...";
    }
    
    /*
     * @Description: 降级测试（RT+时间窗口）
     * @Method: stock
     * @Param: []
     * @Update:
     * @since: 1.0.0
     * @Return: java.lang.String
     *
     */
    @GetMapping("/stock")
    @SentinelResource(value = "stock")
    public String stock() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "库存扣减成功...";
    }
    
    /*
     * @Description: 降级规则（异常比例）
     * @Method: exceptionPercent
     * @Param: []
     * @Update:
     * @since: 1.0.0
     * @Return: java.lang.String
     *
     */
    @GetMapping("/ex")
    @SentinelResource(value = "ex", fallback = "testFallback", blockHandler = "testBlockHandler")
    public String exceptionPercent() {
        int age = 10 / 0;
        return "异常比例达到阈值....";
    }
    
    /*
     * @Description: 出现异常调用当前方法
     * @Method: testFallback
     * @Param: [e]
     * @Update:
     * @since: 1.0.0
     * @Return: java.lang.String
     *
     */
    public String testFallback(Throwable e) {
        if (e instanceof RuntimeException) {
            log.error(e.getMessage());
        }
        
        return "异常降级....";
    }
    /*
     * @Description: 达到流控阈值调用当前方法
     * @Method:
     * @Param:
     * @Update:
     * @since: 1.0.0
     * @Return:
     *
     */
    
    public String testBlockHandler(BlockException e) {
        return "流控降级（限流）";
    }
    
    /*
     * @Description: 热点规则
     * @Method: hot
     * @Param: [id]
     * @Update:
     * @since: 1.0.0
     * @Return: java.lang.String
     *
     */
    @GetMapping("/{name}")
    public String hot(@PathVariable String name) {
        return sentinelService.hot(name);
        
    }
    
    // @GetMapping("/{name}")
    public String hot1(@PathVariable String name) {
        return name;
        
    }
    
    
    
    /*
     * @Description: 授权规则测试
     * @Method:
     * @Param:
     * @Update:
     * @since: 1.0.0
     * @Return:
     *
     */
    
    @GetMapping("/auth")
    public String auth(String auth) {
        return auth;
    }
    
    public String blockHandlerExceptions() {
        
        return "下单人数过多，请稍等一会儿...";
    }
    
}
